package zj.it.bhne.huanxin.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;

import zj.it.bhne.huanxin.R;
import zj.it.bhne.huanxin.myuntils.HuanApplication;

public class HomePageActivity extends AppCompatActivity {

    private static final int sleepTime = 2000;
    private Context context;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_home_page);
        super.onCreate(arg0);
        context = this;
        RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.splash_root);
        TextView versionText = (TextView) findViewById(R.id.tv_version);

        versionText.setText(getVersion());
        AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
        animation.setDuration(1500);
        rootLayout.startAnimation(animation);
    }

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            public void run() {
                if (HuanApplication.isLoggedIn()) {
                    /*// auto login mode, make sure all group and conversation is loaed before enter the main screen
                    long start = System.currentTimeMillis();
                    EMClient.getInstance().chatManager().loadAllConversations();
                    EMClient.getInstance().groupManager().loadAllGroups();
                    long costTime = System.currentTimeMillis() - start;
                    //wait
                    if (sleepTime - costTime > 0) {
                        try {
                            Thread.sleep(sleepTime - costTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    String topActivityName = EasyUtils.getTopActivityName(EMClient.getInstance().getContext());
                    if (topActivityName == null) {
                        //enter main screen

                    }  // nop // avoid main screen overlap Calling Activity*/
                    startActivity(new Intent(context, HomeActivity.class));
                    //finish();
                }else {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException ignored) {
                    }
                    finish();
                    startActivity(new Intent(context, MainActivity.class));
                    //finish();
                }
            }
        }).start();

    }

    /**
     * get sdk version
     */
    private String getVersion() {
        return EMClient.getInstance().VERSION;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
